var express = require('express');
var path = require('path');
var users = require('./routes/users');
var bodyParser = require('body-parser');
const expressJwt = require('express-jwt')
const {cors} = require('./middleware/middleware')
const data = require('./routes/data')
var session = require('express-session');


var app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(cors)
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
const secretKey = '_wz824_'
app.use(expressJwt({secret:secretKey,algorithms: ['HS256'],credentialsRequired:true}).unless({path:[/^\/user\//]}))
app.use(session({
  secret: '_wz824_',
  cookie: {maxAge: 1000*60 },  //设置maxAge是80000ms，即80s后session和相应的cookie失效过期
  resave: false,
  saveUninitialized: true,
}));


app.use('/user',users);
app.use('/data',data)



app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
  });
  
  // error handler
  app.use(function(err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};
  
    // render the error page
    res.status(err.status || 500);
    res.render('error');
  });
module.exports = app;
